package com.community.common.controller;

import com.community.common.pojo.RoleConst;
import com.community.common.pojo.domain.Community;
import com.community.common.pojo.domain.ManagerUser;
import com.community.common.pojo.domain.Menu;
import com.community.common.service.CommunityService;
import com.community.common.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("page")
public class PageController {

    @Resource
    private MenuService menuService;
    @Resource
    private CommunityService communityService;

    /**
     * 登录页
     */
    @RequestMapping("login")
    public String login(HttpServletRequest request) {
        return "common/login";
    }

    /**
     * 首页
     */
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpSession session) {
        ManagerUser user = (ManagerUser) session.getAttribute("loginUser");
        if (user==null){
            return "common/login";
        }
        Integer roleId = user.getRole();
        String mainSrc;
        if (roleId == RoleConst.admin) {
            mainSrc = "/page/community";
        } else {
            mainSrc = "/page/publicUserList";
        }
        request.setAttribute("mainSrc", mainSrc);
        return "manage/index";
    }

    /**
     * 获取头页面
     */
    @RequestMapping("top")
    public String getTop(HttpServletRequest request) {
        return "common/top";
    }

    /**
     * 获取菜单页面
     */
    @RequestMapping("left")
    public String getLeft(HttpServletRequest request, HttpSession session) {
        ManagerUser user = (ManagerUser) session.getAttribute("loginUser");
        Integer roleId = user.getRole();
        String mainSrc;
        if (roleId == RoleConst.admin) {
            mainSrc = "/page/community";
        } else {
            mainSrc = "/page/publicUserList";
        }
        List<Menu> parentMenus = menuService.queryFirstMenu(roleId);
        if (parentMenus.size() > 0) {
            for (Menu parentMenu : parentMenus) {
                List<Menu> childMenus = menuService.queryChildMenu(roleId, parentMenu.getId());
                childMenus.forEach(childMenu -> {
                    if (StringUtils.equals(mainSrc, childMenu.getMenuUrl())) {
                        childMenu.setOpen(true);
                        parentMenu.setOpen(true);
                    }
                });
                parentMenu.setChildMenus(childMenus);
            }
        }
        request.setAttribute("menus", parentMenus);
        return "common/left";
    }

    /**
     * 小区信息管理
     */
    @RequestMapping("/community")
    public String community(HttpSession session) {
        return "manage/community";
    }

    /**
     * 相机管理
     */
    @RequestMapping("/camera")
    public String camera(HttpSession session, Map<String, Object> map) {
        List<Community> communityList = communityService.queryAllCommunity(null);
        map.put("community", communityList);
        return "manage/camera";
    }

    /**
     * 相机管理
     */
    @RequestMapping("/cameraForWuYe")
    public String cameraForWuYe(HttpSession session, Map<String, Object> map) {
        List<Community> communityList = communityService.queryAllCommunity(null);
        map.put("community", communityList);
        return "manage/cameraForWuYe";
    }

    /**
     * 访客记录
     */
    @RequestMapping("/visitRecord")
    public String visitRecord(HttpSession session) {
        return "manage/visitRecord";
    }
    /**
     * 好友查询
     */
    @RequestMapping("/friend")
    public String firend(HttpSession session) {
        return "manage/friend";
    }

    /**
     * 黑名单管理
     */
    @RequestMapping("/publicUserList")
    public String publicUserList(HttpSession session) {
        return "manage/publicUserList";
    }

    @RequestMapping("/accountManage")
    public String accountManage(HttpSession session, Map<String, Object> map) {
        List<Community> communityList = communityService.queryAllCommunity(null);
        map.put("community", communityList);
        return "manage/accountManage";
    }

}
